<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/css/plugins/layui/css/layui.css}">
    <style>
        body {
            padding: 10px;
        }

        .layui-form-item.layui-inline {
            margin: 10px 0px;
            padding-left: 15px;
        }

        #condition,
        #tableDiv {
            background: #fff;
        }
    </style>
</head>

<body>
<form id="condition">
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" placeholder="请输入权限名称" id="menuName" name="menuName" class="layui-input">
            </div>
            <button type="button" id="search" class="layui-btn">查询</button>
            <button type="reset" id="reset" class="layui-btn">重置</button>
        </div>
    </div>
</form>

<div id="tableDiv">
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="add">
        <i class="glyphicon glyphicon-plus"></i>
        新增</button>
    <button class="layui-btn layui-btn-sm" lay-event="update">
        <i class="glyphicon glyphicon-edit"></i>
        修改</button>
    <button class="layui-btn layui-btn-sm" lay-event="del">
        <i class="glyphicon glyphicon-trash"></i>
        删除</button>
</script>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="more">
            更多
            <i class="layui-icon layui-icon-down"></i>
        </a>
    </div>
</script>
<script th:src="@{/js/plugins/layui/layui.js}"></script>
<script>
    layui.use(function () {
        var $ = layui.$;
        var table = layui.table;
        var dropdown = layui.dropdown;

        // 创建渲染实例
        table.render({
            elem: '#test',
            url: '/permission/list', // 此处为静态模拟数据，实际使用时需换成真实接口
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                // 右上角工具图标
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips',
                onClick: function (obj) {
                    // 2.9.12+
                    layer.alert('自定义工具栏图标按钮');
                }
            }],
            css: [
                // 重设当前表格样式
                '.layui-table-tool-temp{padding-right: 145px;}'
            ].join(''),
            cellMinWidth: 80,
            page: true,
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', fixed: 'left', width: 80, title: 'ID', sort: true, totalRow: '合计：'},
                    {field: 'menuName', title: '权限名称'},
                    {field: 'menuType', title: '权限类型'},
                    {field: 'url', title: '请求地址'},
                    {field: 'perms', title: '权限标识'},
                    {fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo'}
                ]
            ],
            error: function (res, msg) {
                console.log(res, msg)
            }
        });

        // 工具栏事件
        table.on('toolbar(test)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var data = checkStatus.data; // 获取选中的数据
            var othis = lay(this);
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加权限',
                        type: 2,
                        area: ['70%', '80%'],
                        content: '/permission/toadd',
                        fixed: false, // 不固定
                        maxmin: true,
                        shadeClose: true,
                        btn: ['立即提交', '取消'],
                        btnAlign: 'c',
                        yes: function (index, layero) {
                            // 获取 iframe 的窗口对象
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            let menuName = iframeWin.$('#menuName');
                            let menuType = iframeWin.$('#menuType');
                            let pId = iframeWin.$('#pId');
                            let url = iframeWin.$('#url');
                            let perms = iframeWin.$('#perms');
                            if ($.trim(menuName.val()) === '') {
                                layer.msg('权限名称不能为空', {
                                    icon: 5
                                });
                                return menuName.focus();
                            }
                            if ($.trim(menuType.val()) === '') {
                                layer.msg('菜单类型不能为空', {
                                    icon: 5
                                });
                                return menuType.focus();
                            }
                            if (menuType.val() === 'M') {
                                if ($.trim(pId.val())!== '') {
                                    layer.msg('菜单类型为 M 时，父级菜单应为空', {
                                        icon: 5
                                    });
                                    return pId.focus();
                                }
                            } else {
                                if ($.trim(pId.val()) === '') {
                                    layer.msg('父级菜单不能为空', {
                                        icon: 5
                                    });
                                    return pId.focus();
                                }
                            }
                            if ($.trim(url.val()) === '') {
                                layer.msg('请求地址不能为空', {
                                    icon: 5
                                });
                                return url.focus();
                            }
                            if (menuType.val()!== 'M' && $.trim(perms.val()) === '') {
                                layer.msg('权限标识不能为空', {
                                    icon: 5
                                });
                                return perms.focus();
                            }
                            $.ajax({
                                url: '/permission/addSave',
                                type: 'post',
                                data: {
                                    menuName: menuName.val(),
                                    menuType: menuType.val(),
                                    parentId: pId.val() === ''? 0 : pId.val(),
                                    url: url.val(),
                                    perms: perms.val()
                                },
                                dataType: 'json',
                                success: function (res) {
                                    layer.close(index);
                                    if (res.code === 0) {
                                        layer.msg(res.msg);
                                        // 刷新表格
                                        table.reload('test');
                                    }
                                }
                            })
                        }
                    });
                    break;
                case 'update':
                    if (data.length!== 1) return layer.msg('请选择一行进行修改', {
                        icon: 5
                    });
                    layer.open({
                        title: '编辑权限',
                        type: 2,
                        area: ['70%', '80%'],
                        content: '/permission/toupdate/' + checkStatus.data[0].id,
                        fixed: false, // 不固定
                        maxmin: true,
                        shadeClose: true,
                        btn: ['立即提交', '取消'],
                        btnAlign: 'c',
                        yes: function (index, layero) {
                            // 获取 iframe 的窗口对象
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            let menuName = iframeWin.$('#update_menuName');
                            let menuType = iframeWin.$('#update_menuType');
                            let pId = iframeWin.$('#update_pId');
                            let url = iframeWin.$('#update_url');
                            let perms = iframeWin.$('#update_perms');
                            if ($.trim(menuName.val()) === '') {
                                layer.msg('权限名称不能为空', {
                                    icon: 5
                                });
                                return menuName.focus();
                            }
                            if ($.trim(menuType.val()) === '') {
                                layer.msg('菜单类型不能为空', {
                                    icon: 5
                                });
                                return menuType.focus();
                            }
                            if (menuType.val() === 'M') {
                                if ($.trim(pId.val())!== '') {
                                    layer.msg('菜单类型为 M 时，父级菜单应为空', {
                                        icon: 5
                                    });
                                    return pId.focus();
                                }
                            } else {
                                if ($.trim(pId.val()) === '') {
                                    layer.msg('父级菜单不能为空', {
                                        icon: 5
                                    });
                                    return pId.focus();
                                }
                            }
                            if ($.trim(url.val()) === '') {
                                layer.msg('请求地址不能为空', {
                                    icon: 5
                                });
                                return url.focus();
                            }
                            if (menuType.val()!== 'M' && $.trim(perms.val()) === '') {
                                layer.msg('权限标识不能为空', {
                                    icon: 5
                                });
                                return perms.focus();
                            }
                            $.ajax({
                                url: '/permission/updateSave',
                                type: 'post',
                                data: {
                                    id: checkStatus.data[0].id,
                                    menuName: menuName.val(),
                                    menuType: menuType.val(),
                                    parentId: pId.val() === ''? 0 : pId.val(),
                                    url: url.val(),
                                    perms: perms.val()
                                },
                                dataType: 'json',
                                success: function (res) {
                                    layer.close(index);
                                    if (res.code === 0) {
                                        layer.msg(res.msg);
                                        // 刷新表格
                                        table.reload('test');
                                    }
                                }
                            })
                        }
                    });
                    break;
                case 'del':
                    if (data.length!== 1) return layer.msg('请选择一行进行删除', {
                        icon: 5
                    });
                    layer.confirm('确定删除所选权限吗？', function (index) {
                        $.ajax({
                            url: '/permission/delete/' + checkStatus.data[0].id,
                            type: 'post',
                            dataType: 'json',
                            success: function (res) {
                                layer.close(index);
                                if (res.code === 0) {
                                    layer.msg(res.msg);
                                    table.reload('test');
                                }
                            }
                        })
                    });
                    break;
            }
        });

        // 触发单元格工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑权限 - ID:' + data.id,
                    type: 2,
                    area: ['70%', '70%'],
                    content: '/permission/toupdate/' + data.id,
                    fixed: false, // 不固定
                    maxmin: true,
                    shadeClose: true,
                    btn: ['立即提交', '取消'],
                    btnAlign: 'c',
                    yes: function (index, layero) {
                        // 获取 iframe 的窗口对象
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        let menuName = iframeWin.$('#menuName');
                        let menuType = iframeWin.$('#menuType');
                        let pId = iframeWin.$('#pId');
                        let url = iframeWin.$('#url');
                        let perms = iframeWin.$('#perms');
                        if ($.trim(menuName.val()) === '') {
                            layer.msg('权限名称不能为空', {
                                icon: 5
                            });
                            return menuName.focus();
                        }
                        if ($.trim(menuType.val()) === '') {
                            layer.msg('菜单类型不能为空', {
                                icon: 5
                            });
                            return menuType.focus();
                        }
                        if (menuType.val() === 'M') {
                            if ($.trim(pId.val())!== '') {
                                layer.msg('菜单类型为 M 时，父级菜单应为空', {
                                    icon: 5
                                });
                                return pId.focus();
                            }
                        } else {
                            if ($.trim(pId.val()) === '') {
                                layer.msg('父级菜单不能为空', {
                                    icon: 5
                                });
                                return pId.focus();
                            }
                        }
                        if ($.trim(url.val()) === '') {
                            layer.msg('请求地址不能为空', {
                                icon: 5
                            });
                            return url.focus();
                        }
                        if (menuType.val()!== 'M' && $.trim(perms.val()) === '') {
                            layer.msg('权限标识不能为空', {
                                icon: 5
                            });
                            return perms.focus();
                        }
                        $.ajax({
                            url: '/permission/updateSave',
                            type: 'post',
                            data: {
                                id: data.id,
                                menuName: menuName.val(),
                                menuType: menuType.val(),
                                parentId: pId.val() === ''? 0 : pId.val(),
                                url: url.val(),
                                perms: perms.val()
                            },
                            dataType: 'json',
                            success: function (res) {
                                layer.close(index);
                                if (res.code === 0) {
                                    layer.msg(res.msg);
                                    // 刷新表格
                                    table.reload('test');
                                }
                            }
                        })
                    }
                });
            } else if (obj.event === 'more') {
                // 更多 - 下拉菜单
                dropdown.render({
                    elem: this,
                    show: true,
                    data: [{
                        title: '查看',
                        id: 'detail'
                    }, {
                        title: '删除',
                        id: 'del'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'detail') {
                            layer.open({
                                title: '查看权限详情',
                                type: 2,
                                area: ['70%', '70%'],
                                content: '/permission/toinfo/' + data.id,
                                fixed: false,
                                maxmin: true,
                                shadeClose: true,
                                btn: ['关闭'],
                                btnAlign: 'c'
                            });
                        } else if (menudata.id === 'del') {
                            layer.confirm('确定删除该权限吗？', function (index) {
                                $.ajax({
                                    url: '/permission/delete/' + data.id,
                                    type: 'post',
                                    dataType: 'json',
                                    success: function (res) {
                                        layer.close(index);
                                        if (res.code === 0) {
                                            layer.msg(res.msg);
                                            table.reload('test');
                                        }
                                    }
                                })
                            });
                        }
                    },
                    align: 'right',
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);'
                })
            }
        });

        $("#search").click(function () {
            var menuName = $("#menuName").val();
            var data = {menuName: menuName};
            table.reload('test', {
                where: data
            })
        })
        $("#reset").click(function () {
            table.reload('test', {
                where: {}
            });
        })
    });
</script>
</body>

</html>